<?php
/*********************************************************************\
*  Copyright (c) 1998-2013, TH. All Rights Reserved.
*  Author :lwj
*  FName  :ruku.php
*  Time   :2019.10.22
*  Remark :样品篮
\*********************************************************************/
FLEA::loadClass('TMIS_Controller');
class Controller_Sample_Cart  extends TMIS_Controller {


    function __construct() {
        $this->_modelExample = FLEA::getSingleton('Model_Sample_Ruku');
        $this->_subModel = FLEA::getSingleton('Model_Sample_Ruku2Product');
    }

    //用于添加到篮子中
    function actionAdd(){
        $row = array();
        $smarty = & $this->_getView();
        $smarty->assign('title','样品批量操作');
        $smarty->assign('row',$row);
        $smarty->assign('sonTpl','Sample/Cart/CartAdd.js');
        $smarty->display('Sample/Cart/CartAdd.tpl');
    }


    //安全验证参数
    function actionGetSampleInfo(){
        $post = $this->axiosPost();
        if(!$post['barcode']){
            $ret = array(
                'success' =>false,
                'msg'     =>'样品编号不能为空',
            );
            echo json_encode($ret);exit;
        }

        $sql = "SELECT x.*,y.proName
        from sample_ruku2product x
        left join jichu_product y on x.productId=y.id
        where x.code='{$post['barcode']}' order by id desc limit 1";
        $tmp = $this->_modelExample->findBySql($sql);
        $bar = $tmp[0];
        if(!$bar['id']){
            $ret = array(
                'success' =>false,
                'msg'     =>'样品编号不存在',
            );
            echo json_encode($ret);exit;
        }

        // 返回样品的信息
        $ret = array(
            'success' =>true,
            'msg'     =>'',
            'sample'  =>$bar,
        );
        echo json_encode($ret);exit;
    }

    /**
     * 导出
     * Time：
     * @author li
     * @param 参数类型
     * @return 返回值类型
    */
    function actionExport(){
        // 先处理数据
        $id = explode(',',strval($_GET['id']));
        $condition = array();
        $condition['in()'] = array('id'=>$id);
        $rows = $this->_modelExample->findAll($condition);
        // dump($rows);exit;

        // 数据处理好了，下面处理导出
        FLEA::loadClass('TMIS_Phpexcel');
        $pexcel = & new TMIS_Phpexcel(array(),'Lib/App/Template/Sample/baojia.xls');
        $pexcel->getSheet(0);
        //先处理循环行
        //先插入行，然后对每行进行处理
        $currentRow = 2;
        //模版预留了两个空行
        $needRows = count($rows) - 1;
        // 开始插入行:这里支持一次性插入和一行一行插入，一次性插入方便处理合并航的情况，减少循环代码
        $needRows > 0 && $pexcel->insertRow($currentRow+1,$needRows);

        $_modelKind = FLEA::getSingleton('Model_Jichu_ProKind');

        //处理每行的数据
        foreach ($rows as $key => & $row) {
            foreach ($row['Products'] as $key => & $v) {
                // 查找坯布价格
                $sql = "SELECT * from jichu_product where id='{$v['productId']}'";
                $tmp = $this->_modelExample->findBySql($sql);
                $pro = $tmp[0];

                $fileds = $_modelKind->getFiledsKeyValue($pro['kindId']);
                $fileds = array_flip($fileds);
                $v['materialPrice'] = round($pro['materialPrice'] , 6);
                // dump($fileds);exit;
                $pexcel->setCell('A'.$currentRow ,$v['code']);
                $pexcel->setCell('B'.$currentRow ,$pro[$fileds['成分']]);
                $pexcel->setCell('C'.$currentRow ,$pro[$fileds['纱支']]);
                $pexcel->setCell('D'.$currentRow ,$pro[$fileds['克重']]);
                $pexcel->setCell('E'.$currentRow ,$pro[$fileds['门幅'] ? $fileds['门幅'] : $fileds['有效门幅']]);
                $pexcel->setCell('F'.$currentRow ,$v['materialPrice']);

                // 插入产品信息行
                $currentRow++;
            }
        }
        //渲染
        $pexcel->render();
        // $pexcel->replaceVar($row);

        $pexcel->export('报价单 - '.date('Y-m-d'));
        exit;
    }
}

?>